package com.workcell.workdata.im.component.observer;

import com.workcell.workdata.im.component.event.AppMessageEvent;
import com.workcell.workdata.im.component.event.FriendEvent;
import com.workcell.workdata.im.component.event.GroupEvent;
import com.workcell.workdata.im.component.event.ImageUploadEvent;
import com.workcell.workdata.im.constant.MessageAction;
import com.workcell.workdata.im.entity.EventMessage;
import com.workcell.workdata.im.entity.Message;
import com.workcell.workdata.im.repository.GroupRobotRepository;
import org.springframework.context.ApplicationContext;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class ImageChangeObserver {

    @Resource
    private ApplicationContext applicationContext;

    @Resource
    private GroupRobotRepository groupRobotRepository;

    @EventListener(value = ImageUploadEvent.class,condition = "#root.args[0].isUserLogo")
    @Async("pushTaskExecutor")
    public void onUserLogoChanged(ImageUploadEvent event){

        Message message = new EventMessage();
        message.setSender(event.getId());
        message.setAction(MessageAction.ACTION_102);

        applicationContext.publishEvent(new FriendEvent(message));
    }

    @EventListener(value = ImageUploadEvent.class,condition = "#root.args[0].isGroupLogo")
    @Async("pushTaskExecutor")
    public void onGroupLogoChanged(ImageUploadEvent event){
        Message message = new EventMessage();
        message.setSender(event.getId());
        message.setAction(MessageAction.ACTION_306);
        applicationContext.publishEvent(new GroupEvent(message));
    }

    @EventListener(value = ImageUploadEvent.class,condition = "#root.args[0].isRobotLogo")
    @Async("pushTaskExecutor")
    public void onRobotLogoChanged(ImageUploadEvent event){
        Long groupId = groupRobotRepository.findGroupId(event.getId());
        GroupEvent groupEvent = new GroupEvent(groupId);
        groupEvent.setAction(MessageAction.ACTION_315);
        groupEvent.setContent(String.valueOf(event.getId()));
        groupEvent.setReplaceable(false);
        applicationContext.publishEvent(event);
    }

    @EventListener(value = ImageUploadEvent.class,condition = "#root.args[0].isAppLogo")
    @Async("pushTaskExecutor")
    public void onAppLogoChanged(ImageUploadEvent event){

        Message message = new EventMessage();
        message.setSender(event.getId());
        message.setAction(MessageAction.ACTION_204);

        applicationContext.publishEvent(new AppMessageEvent(message));

    }

    @EventListener(value = ImageUploadEvent.class,condition = "#root.args[0].isMomentWallpaper")
    @Async("pushTaskExecutor")
    public void onMomentWallpaperChanged(ImageUploadEvent event){
        Message message = new EventMessage();
        message.setSender(event.getId());
        message.setAction(MessageAction.ACTION_504);
        applicationContext.publishEvent(new FriendEvent(message));
    }
}
